<?php 
namespace Home\Controller;
use Think\Controller;
	class AccountController extends InitializeController{

		public function index(){
			$this->display();
		}

		public function runLogin(){
			//与后台登录冗余,Common function.php尝试优化失败
			//都用查询尚未优化?
			$db = M('User');
			$loginVerify = array(
                array('username','require','请填账号！'), //默认情况下用正则进行验证
                array('password','require','请填写密码！'), //默认情况下用正则进行验证
            );
			if (I('submit') == '立即登录') {
					//如果用户提交数据
	            $model = D("User");
	            if (!$model->validate($loginVerify)->create()) {
	                // 如果创建失败 表示验证没有通过 输出错误提示信息
	                $this->error($model->getError());
	                exit();
	            }else {
	            	$where['username'] = I('username');
	            	//此处where("username=%s",I('username'))不好使,原因未测试
	                if ($name = $model->where($where)->select()) {
	                	$pwd = $model->where($where)->field('password')->select();
	                	//可查询多个字段
	                	if($pwd['0']['password'] == md5(I('password'))){
	                		//验证验证码是否正确
					        // if(!($this->check_verify(I('verify','','strtolower')))){
					        //     $this->error('验证码错误');
					        // }else{
	                			$role = $model->where($where)->field('role_id')->select();
	                				session_start();
	                				session_destroy('mallUserId');
						        	$userId = $model->where($where)->field('user_id')->select();
						        	session('mallUserId',$userId['0']['user_id']);
						        	//BUG!!!!!!!!!!!!!!!!!!!!!!
						        	// $judge = $db->where("user_id=%d",$userId['0']['user_id'])->setField(array('logintime','loginip'),array(date('Y-m-d H:i:s',time()),$_SERVER['SERVER_ADDR']));
						        	// var_dump($judge);
						        	if ($judge = $db->where("user_id=%d",$userId['0']['user_id'])->setField(array('logintime'=>date("Y-m-d H:i:s",time()),'loginip'=>$_SERVER["REMOTE_ADDR"]))) {
						        		//更新数据库此用户上次登录时间、ip
						        		$this->success("登陆成功!",U('Index/index'));
						        	 } 
					        // }
	                	}else{
	                		$this->error("密码错误!");
	                	}
	                }else{
	                	$this->error("账号不存在!");
	                }
	            }
			}
		}

		public function register(){
			$db = M('User');
			if (I('submit') == '立即注册') {

			 	$model = D("User");
	            if (!$model->create()) {
	                // 如果创建失败 表示验证没有通过 输出错误提示信息
	                $this->error($model->getError());
	                exit();
	            }else {
	            	$data = array(
						'username' => I('username'),
						'password' => md5(I('password')),
					);
					$adminDate = array(
						'account' => I('username'),
						'password' => md5(I('password')),
						'create_time' => time(),
						);

					if (!($this->check_verify(I('verify','','strtolower')))) {
						$this->error('验证码错误');
					}
					if ($judge = $db->data($data)->add() && $judge2 = M('admin')->data($adminDate)->add()) {
						$where['username'] = I('username');
						$user_id = $db->where($where)->field('user_id')->select();
						$data_role_user = array(
							'role_id' => 32,
							'user_id' => $user_id['0']['user_id'],
						);
						$date_group_access = array(
							'uid' => $user_id['0']['user_id'],
							'group_id' => 32,
							);
						if ($judge = M('role_user')->data($data_role_user)->add()&&$judge2 = M('auth_group_access')->data($date_group_access)->add()) {
							$this->success('注册成功!',U('Account/index'));
						}
					}
	            }
			}else{
				$this->display();
			}
		}

		protected function check_verify($code){
	        $verify = new \Think\Verify();
	        return $verify->check($code);
	    }

		public function verify(){
	        $Verify = new \Think\Verify();
	        $Verify->codeSet = '0123456789';
	        $Verify->fontSize = 13;
	        $Verify->length = 4;
	        $Verify->entry();
	    }

	    public function userInfo(){
	    	$db = M('userinfo');
	    	$this->userinfo = $db->select();
	    	$this->display();
	    }

	    public function addInfo(){
	    	$db = M('userinfo');
	    	if (I('get.action') == 'ajax') {
	    		$data = array(
	    			'user_id' => $_SESSION['mallUserId'],
	    			'province' => I('province'),
	    			'city' => I('city'),
	    			'region' => I('region'),
	    			'address' =>I('address'),
	    			'email' =>I('email'),
	    			'realname' => I('realname'),
	    			'tell' => I('tell'),
	    			);
	    		if ($db->data($data)->add()) {
	    			$arr['success'] = 1;
	    			$arr['province'] = I('province');
	    			$arr['city'] = I('city');
	    			$arr['region'] = I('region');
	    			$arr['address'] = I('address');
	    			$arr['email'] = I('email');
	    			$arr['realname'] = I('realname');
	    			$arr['tell'] = I('tell');
					echo json_encode($arr);	//将数值转换成json数据存储格式	

	    		}
	    	}
	    }

	    public function uinfodel(){
	    	if ($judge = M('userinfo')->where("id=%d",I('get.id'))->delete()) {
	    		header("Location: ".U('Account/userInfo'));
	    	}
	    }
	    
	}
 ?>